<?php
namespace app\member\controller;
use think\Controller;
use app\member\Lib\Weixin;
use think\Log;
class Base extends Controller {
    public function __construct(){
        parent::__construct();
        $user = getSession("user");
        if (empty($user)){
            
            $host = $_SERVER['HTTP_HOST'];
            if ($host=="crm.zzsmsc.cn:8080"){
                $user = [
                    'userid'=>'01491',
                    'name'=>'陈建飞',
                    'mobile'=>'15981877321',
                    'avatar'=>'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1521800220716&di=590a8ee201fe1ecd2d3eb4750833c8a6&imgtype=0&src=http%3A%2F%2Fwww.ld12.com%2Fupimg358%2Fallimg%2F20160629%2F22484718649769.jpg'
                ];
                setSession("user", $user);
                goto index;
            }
            if (!input("code")){
                $this->error("请通过微信企业号登录！");
            }
            $code = input("code");
            $weixin = new Weixin(config("corpId"), config("secret"), config("agentid"));
            $userinfo = $weixin->getUserInfo($code);
            if ($userinfo['errcode']==40029){
                $this->error("请在微信企业号中重新授权登录，ERR:40029!");
            }
            if ($userinfo['errcode']==0){
                Log::write(json_encode($userinfo));
                //通过授权获取登录者信息
                if (array_key_exists("user_ticket", $userinfo)){
                    $userdetail = $weixin->getUserDetail($userinfo['user_ticket']);
                    setSession("user", $userdetail);
                }else{
                    $this->error("请先加入肯通郑州企业号！");
                }

                //通过userid获取登录者信息
                /*else if(array_key_exists("UserId", $userinfo)){
                      $userdetail = $weixin->getUserById($userinfo['userid']);
                     setSession("user", $userdetail);
                    $this->error("请先在微信企业号个人中心中授权登录！");
                }*/
            }
            
        }
        index:
    }
}